<template>
  <Manage>
    <template #upCard>
      <el-form label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="任务编号">
              <el-input v-model="page.id" placeholder="请输入任务编号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="任务分配状态">
              <el-select v-model="page.assignedStatus" style="width:100%" clearable>
                <el-option label="已分配" :value="2" />
                <el-option label="待人工分配" :value="3" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="满载状态">
              <el-select v-model="page.loadingStatus" style="width:100%" clearable>
                <el-option label="满载" :value="2" />
                <el-option label="空载" :value="3" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车牌号码">
              <el-input v-model="page.licensePlate" placeholder="请输入车牌号码" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="起始地机构">
              <Treeselect
                v-model="page.startAgencyId"
                :options="tree"
                :normalizer="normalizer"
                placeholder="请选择起始地机构"
                :append-to-body="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="目的地机构">
              <Treeselect
                v-model="page.endAgencyId"
                :options="tree"
                :normalizer="normalizer"
                placeholder="请选择目的地机构"
                :append-to-body="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-button type="primary" size="" @click="search">搜索</el-button>
            <el-button @click="reset">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </template>
    <template #midCard>
      <TabChange :tabs="setMap.transportTaskStatus" :status-count="statusCount" @changeItem="changeItem" />
    </template>
    <template #downCard>
      <el-table
        v-loading="loading"
        :data="transportTaskData"
        stripe
        :header-cell-style="{background:'#f8faff',color:'#818693'}"
      >
        <el-table-column type="index" label="序号" />
        <el-table-column prop="id" label="运输任务编号" />
        <el-table-column label="车牌号码">
          <template #default="{row}">
            <span>{{ row.truck.licensePlate }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="起始地">
          <template #default="{row}">
            <span>{{ row.startAgency.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="startAgencyName" label="目的地">
          <template #default="{row}">
            <span>{{ row.endAgency.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="运输任务状态">
          <template #default="{row}">
            <span>{{ row.status | showTransportTaskStatus }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="loadingStatus" label="车辆装载状态">
          <template #default="{row}">
            <span>{{ row.loadingStatus===2?'满载':'空载' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="assignedStatus" label="任务分配状态">
          <template #default="{row}">
            <span>{{ row.assignedStatus===2?'已分配':'待人工分配' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="planDepartureTime" label="计划发车时间" />
        <el-table-column prop="actualDepartureTime" label="实际发生时间" />
        <el-table-column prop="updated" label="更新时间" />
        <el-table-column label="操作" align="center" width="180" fixed="right">
          <template #default="{row}">
            <!-- <a href="javascript:void(0);" style="color:#419eff;">作业范围分配</a> -->
            <router-link
              style="color:#419eff"
              :to="{path:'/transport/task-detail',query:{id:row.id,type:'运输管理'},}"
            >查看</router-link>
            <div class="line" />
            <el-button type="text" disabled>取消</el-button>
            <div class="line" />
            <el-button type="text" disabled>手动调整</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="center">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="counts"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </template>
  </Manage>
</template>

<script>
import { getOrgizationTree } from '@/api/ogization'
import setMap from '@/constant/setMap'
import { getTransportTask, getTransportTaskCount } from '@/api/transport'
export default {
  filters: {
    showTransportTaskStatus(val) {
      return setMap.transportTaskStatus.find(item => item.id === val)?.item || ''
    }
  },
  data() {
    return {
      setMap,
      page: {
        page: 1,
        pageSize: 10,
        id: null,
        assignedStatus: null,
        loadingStatus: null,
        licensePlate: null,
        startAgencyId: null,
        endAgencyId: null
      },
      status: null,
      statusCount: {},
      counts: 0,
      tree: [],
      transportTaskData: [],
      loading: false
    }
  },
  created() {
    this.initTree()
    this.initTransportTask()
    this.initStatusCount()
  },
  methods: {
    async initStatusCount() {
      this.statusCount = await getTransportTaskCount()
    },
    // 运输任务表格
    async initTransportTask() {
      this.loading = true
      try {
        const res = await getTransportTask({ ...this.page, status: this.status })
        this.transportTaskData = res.items
        this.counts = +res.counts
      } catch (error) {
        this.loading = false
      }
      this.loading = false
    },
    // 下拉树结构
    async initTree() {
      const res = await getOrgizationTree()
      this.tree = JSON.parse(res)
    },
    normalizer(node) {
      // 当子节点也就是children=[]时候去掉子节点
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children && node.children.length > 0 ? node.children : 0
      }
    },
    // 搜索
    search() {
      this.initTransportTask()
    },
    // 分页
    handleSizeChange(val) {
      this.page.pageSize = val
      this.initTransportTask()
    },
    handleCurrentChange(val) {
      this.page.page = val
      this.initTransportTask()
    },
    reset() {
      this.page = {
        page: 1,
        pageSize: 10,
        id: null,
        assignedStatus: null,
        loadingStatus: null,
        licensePlate: null,
        startAgencyId: null,
        endAgencyId: null
      }
      this.initTransportTask()
    },
    // 切换
    changeItem(id) {
      this.status = id
      this.initTransportTask()
    }
  }

}
</script>

<style lang="scss" scoped>
</style>
